import { defineStore } from 'pinia'
// import { getStarList, getVipList } from '@/api/activity'
import { getUserCombines } from '@/api/user'
import { UserTagsType, Member } from '@/types'

export const useMemberStore = defineStore('member', {
	state: () => ({
		vipList: [] as Array<Member>,
		starList: [] as Array<Member>,
		roiDays: 0,
		winRateDays: 0,
		tagList: [] as Array<UserTagsType>
	}),
	actions: {
		async getUserTags(value: any[]) {
			try {
				const ts = Date.now()
				const userIds = value.filter((userId) => {
					const user = this.tagList.find((item) => +item.userId === +userId)
					return !user || ts - user.ts > 60 * 60 * 1000
				})
				if (!userIds.length) return
				const { data } = await getUserCombines({
					userIds: userIds.toString()
				})
				if (!data.feedsLabel.length) return
				this.setUserTags(data.feedsLabel.map((item: any) => ({ ...item, ts })))
			} catch (e) {}
		},
		setUserTags(value: any[]) {
			if (value && value.length) {
				const res = this.tagList.filter(
					({ userId }) => !value.find((item) => +item.userId === +userId)
				)
				this.tagList = [...res, ...value]
			}
		},
		setUserTagsDays(roiDays: number, winRateDays: number) {
			this.roiDays = roiDays
			this.winRateDays = winRateDays
		}
	},
	getters: {
		userTags: (state) => (userId: string | number) =>
			state.tagList.find((item: any) => +item.userId === +userId)
	},
	persist: true
})
